﻿#include "DualCamerasDisplayWidget.h"
#include <QGridLayout>

const int ZOOMFACTOR[20] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200};

DualCamerasDisplayWidget::DualCamerasDisplayWidget(QWidget * parent)
{
    m_zoomFactorIndex = 4;
    for(int i = 0; i < 2; i++)
    {
        m_pView[i] = new Qly::ImageView(this);
        m_pView[i]->setZoomFactorPCT(ZOOMFACTOR[m_zoomFactorIndex]);
    }

    QGridLayout * layout = new QGridLayout;
    layout->addWidget(m_pView[0], 0, 0);
    layout->addWidget(m_pView[1], 0, 1);
    layout->setSizeConstraint(QLayout::SetFixedSize);
    setLayout(layout);

    connect(m_pView[0], &Qly::ImageView::mouseMoved, this, &DualCamerasDisplayWidget::onMouseMoved);
    connect(m_pView[1], &Qly::ImageView::mouseMoved, this, &DualCamerasDisplayWidget::onMouseMoved);
}

void DualCamerasDisplayWidget::onMouseMoved(QPointF &point)
{
    Qly::ImageView * pView = (Qly::ImageView *) sender();

    if(pView == m_pView[0])
    {
        emit mouseMoved(0, point);
    }
    else
    {
        emit mouseMoved(1, point);
    }
}

void DualCamerasDisplayWidget::setRect(int robotNo, QRect leftRect, QRect rightRect)
{
    m_pView[robotNo]->setLeftRect(leftRect);
    m_pView[robotNo]->setRightRect(rightRect);
}

void DualCamerasDisplayWidget::loadSettings(QSettings &settings)
{
    m_zoomFactorIndex = settings.value(QString("Display/ZoomFactor"), 5).toInt();
    for(int i = 0; i < 2; i++)
    {
        m_pView[i]->setZoomFactorPCT(ZOOMFACTOR[m_zoomFactorIndex]);
    }
}

void DualCamerasDisplayWidget::writeSettings(QSettings &settings)
{
    settings.setValue(QString("Display/ZoomFactor"), m_zoomFactorIndex);
}

void DualCamerasDisplayWidget::zoomIn()
{
    m_zoomFactorIndex = qBound(0, m_zoomFactorIndex + 1, 19);
    for(int i = 0; i < 2; i++)
    {
        m_pView[i]->setZoomFactorPCT(ZOOMFACTOR[m_zoomFactorIndex]);
    }
}

void DualCamerasDisplayWidget::zoomOut()
{
    m_zoomFactorIndex = qBound(0, m_zoomFactorIndex - 1, 19);
    for(int i = 0; i < 2; i++)
    {
        m_pView[i]->setZoomFactorPCT(ZOOMFACTOR[m_zoomFactorIndex]);
    }
}

void DualCamerasDisplayWidget::setZoomFactorPCT(int pct)
{
    m_pView[0]->setZoomFactorPCT(pct);
    m_pView[1]->setZoomFactorPCT(pct);
}

void DualCamerasDisplayWidget::attch(Qly::CameraImage_RGB32 *pImage[])
{
    for(int i = 0; i < 2; i++)
    {
        connect(pImage[i], &Qly::CameraImage::imageChanged, m_pView[i], &Qly::ImageView::setImage);
    }
}
